package response;

import core.*;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * Used for sending a response back to the client.
 * Create a version of this for each response type.
 *
 * @author eric, yiyi, Gary
 */
public abstract class GameResponse {

    // The outputstream for this object.
    protected OutputStream output;
    // The data outputstream
    protected DataOutputStream dataOut = null;
    // GameServer object.
    protected GameServer gameServer;
    // Response Code
    protected short code;

    /**
     * Runs this response.
     */
    public void run() {
        try {
            dataOut.write(createResponse());
        } catch (IOException e) {
            gameServer.log(e.getMessage(),"e");
        }
    }

    /**
     * Data package to be sent
     * @return		a byte[] package
     */
    public abstract byte[] createResponse();

    /**
     * Set the game server.
     * @param gameServer	nurse game server reference
     */
    public void set(GameServer gameServer) {
        this.gameServer = gameServer;
    }

    /**
     * Set up the output stream
     * @param output		output stream associate with client socket
     */
    public void setOut(OutputStream output) {
        this.output = output;
        dataOut = new DataOutputStream(output);
    }

    /**
     * Returns the corresponding response code
     *
     * @return code
     */
    public short getCode() {
        return code;
    }
}
